<template>
  <div class="bgc">
    <!-- 购买商品列表区u -->
    <div class="goodslist-item">
      <div class="mui-card" v-for="(item,index) in goodslist" :key="item.id">
        <span style="font-size:14px;font-weight:bold;color:black">{{item.title}}</span>
        <div class="mui-card-content">
          <div class="mui-card-content-inner infos">
            <div class="left-info">
              <mt-switch
                v-model="$store.getters.getGoodsSelected[item.id]"
                @change="selectedChange(item.id,$store.getters.getGoodsSelected[item.id])"
              ></mt-switch>
              <img :src="item.thumb_path" alt width="60px" height="60px" class="imgs">
            </div>

            <div class="right-info">
              <span style="color:red">￥{{item.sell_price}}</span>
              <numbox :initcount="$store.getters.getGoodsCount[item.id]" :goodsid="item.id"></numbox>
              <a href="#" @click.prevent="remove(item.id,index)">删除</a>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 购买商品结算区域     -->
    <div class="goodsadd-item">
      <div class="mui-card xx">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <div class="ada">
              <div class="left">
                <p>总计（不含运费）</p>
                <p>
                  已勾选
                  <span class="num">{{$store.getters.addAmountAndPrice.count}}</span> 件商品，总计￥
                  <span class="num">{{$store.getters.addAmountAndPrice.price}}</span> 元
                </p>
              </div>
              <mt-button type="danger">去结算</mt-button>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- <p>{{$store.getters.getGoodsSelected}}</p> -->
  </div>
</template>

<script>
import numbox from "../subcponents/shopcar.vue";
export default {
  data() {
    return {
      goodslist: []
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
   async getGoodsList() {
      var idArr = [];
      this.$store.state.car.forEach(element => {
        idArr.push(element.id);
      });
      if (idArr.length <= 0) {
        return;
      }
     await this.$http
        .get("api/goods/getshopcarlist/" + idArr.join(","))
        .then(result => {
          if (result.data.status == 0) {
            this.goodslist = result.data.message;
          }
        });
    },
    remove(id, index) {
      //点击删除  根据id值删除
      this.goodslist.splice(index, 1);
      this.$store.commit("removeFromCar", id);
    },
    selectedChange(id, val) {
      // console.log(id,val)
      //每当点击开关获取开关状态 并同步到store中
      this.$store.commit("dateSelected", { id: id, selected: val });
    }
  },
  components: {
    numbox
  }
};
</script>

<style scoped>
* {
  margin: 0 0px;
  padding: 0 2px;
  text-align: center;
}
.bgc{
  background-color: whitesmoke;
  overflow: hidden;
}
.infos {
  display: flex;
}
.left-info {
  display: flex;
  align-items: center;
}
.right-info {
  display: flex;
  align-items: center;
}
.ada {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.num {
  color: red;
  font-weight: bold;
}
.imgs{
  box-shadow: 0 0 2px gainsboro;
}
</style>
